<script setup>
import { RouterLink, RouterView } from 'vue-router'
import { homeRoute } from '@/router/router'
import tools from '@/components/blog-components/tools.vue'

import { ref } from 'vue'

// 计算当前激活的导航项
const route = useRoute()
const activePath = computed(() => route.path)
const headerRef = ref(null)
// 添加移动端菜单状态
const isMenuOpen = ref(false)

// 切换菜单显示/隐藏
const toggleMenu = () => {
    isMenuOpen.value = !isMenuOpen.value
}

// 点击菜单项后关闭菜单（移动端）
const handleNavClick = () => {
    if (window.innerWidth <= 820) {
        isMenuOpen.value = false
    }
}

const homeInfo = ref(computed(() => {
    return {
        title: route.query.title || route.name,
        subtitle: '分享技术，分享生活',
        backgroundImage: new URL('@/assets/images/blog_avatar.jpg', import.meta.url).href
    }
})
)
onMounted(() => {
    headerRef.value = document.querySelector('.header-container')
    let lastScrollTop = 0
    window.addEventListener("scroll", () => {
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0
        if (scrollTop >= 500 && scrollTop > lastScrollTop) {
            headerRef.value.classList.add('sticky')
        } else {
            headerRef.value.classList.remove('sticky')
        }
        lastScrollTop = scrollTop
    })
})
</script>

<template>
    <div class="main-container">
        <div class="main-panel" :style="{ 'background-image': 'url(' + homeInfo.backgroundImage + ')' }">
            <header class="header-container" ref="headerRef">
                <div class="header-panel">
                    <div class="logo">个人博客</div>
                    <div class="header-wrapper" :class="{ 'menu-open': isMenuOpen }">
                        <nav v-for="(nav, index) in homeRoute" :key="index" :class="{ active: activePath === nav.path }"
                            @click="handleNavClick">
                            <RouterLink :to='nav.path'>{{ nav.name }}</RouterLink>
                            <!-- <ul>
                                    <li></li>
                                </ul> -->
                        </nav>
                    </div>
                    <div class="more" :class="{ 'open': isMenuOpen }" @click="toggleMenu">
                        <div class="hamburger">
                            <span></span>
                            <span></span>
                            <span></span>
                        </div>
                    </div>
                </div>

            </header>
            <div class="home-info">
                <div class="home-info-title">{{ homeInfo.title }}</div>
                <div class="home-info-subtitle">分享技术，分享生活</div>
            </div>
        </div>
        <RouterView />
        <tools />
    </div>
</template>

<style lang="scss" scoped>
@use '@/assets/variables.scss' as *;

.main-container {}

.main-panel {
    height: 500px;
    // margin-bottom: 10px;
    background-color: var(--header-color);
    font-family: "Courier New", Courier, monospace;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    // backdrop-filter: brightness(0.8);
    // filter: brightness(0.8);
}

.content-area {}

.header-container {
    height: 50px;
    background: var(--header-color);
    width: 100%;
    display: flex;
    justify-content: center;
    transition: transform 0.3s ease-in-out;
    position: fixed;

    // backdrop-filter: saturate(180%) blur(10px);

    .more {
        width: 30px;
        height: 30px;
        display: none;
        align-items: center;
        justify-content: center;
        position: relative;
        cursor: pointer;
    }

    .header-panel {
        display: flex;
        align-items: center;
        width: 1200px;
        height: 100%;

        .logo {
            width: 150px;
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    }

    &.sticky {
        transform: translateY(-100%);
    }
}

.header-panel .header-wrapper {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    nav {
        position: relative;
        height: 100%;
        display: flex;
        align-items: center;

        a {
            width: 100px;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: inherit;
        }

        ul {
            position: absolute;
            left: 0;
            top: 100%;
            width: 100%;
            visibility: hidden;
            transform: translateY(2px);
            opacity: 0;
            transition: all 0.3s ease-in-out;

            li {
                background: #000;
                height: 48px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
            }
        }

        &:hover {
            cursor: pointer;
            background-color: var(--header-hover-color);

            &>ul {
                visibility: visible;
                opacity: 1;
                transform: translateY(0px);

                li {
                    &:hover {
                        background-color: rgba(0, 0, 0, 0.8);
                    }
                }
            }
        }

        &.active {
            background-color: var(--header-hover-color);
        }
    }
}

.home-info {
    width: 100%;
    height: calc(100% - 60px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 36px;

    .home-info-title {
        font-size: 36px;
        font-weight: 700;
    }

    .home-info-subtitle {
        font-size: 20px;
        font-weight: 700;
    }
}

@media (max-width: 820px) {
    .header-panel {
        position: relative;
        justify-content: space-between;
        width: 100%;

        .more {
            width: 100px;
            display: flex !important;

            .hamburger {
                width: 24px;
                height: 18px;
                position: relative;
                transform: rotate(0deg);
                transition: .5s ease-in-out;

                span {
                    display: block;
                    position: absolute;
                    height: 2px;
                    width: 100%;
                    background: #333;
                    border-radius: 2px;
                    opacity: 1;
                    left: 0;
                    transform: rotate(0deg);
                    transition: .25s ease-in-out;

                    &:nth-child(1) {
                        top: 0px;
                    }

                    &:nth-child(2) {
                        top: 8px;
                    }

                    &:nth-child(3) {
                        top: 16px;
                    }
                }
            }

            // 打开状态（变成叉号）
            &.open {
                .hamburger {
                    span {
                        &:nth-child(1) {
                            top: 8px;
                            transform: rotate(135deg);
                        }

                        &:nth-child(2) {
                            opacity: 0;
                            left: -60px;
                        }

                        &:nth-child(3) {
                            top: 8px;
                            transform: rotate(-135deg);
                        }
                    }
                }
            }
        }
    }

    .header-panel .header-wrapper {
        position: absolute;
        top: 100%;
        left: 0;
        margin: 0;
        width: 100%;
        height: 0;
        flex-direction: column;
        background: var(--bg-color);
        overflow: hidden;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        transition: height 0.3s ease-in-out;

        // 菜单打开时的状态
        &.menu-open {
            height: 180px;
        }

        nav {
            width: 100%;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);

            a {
                justify-content: flex-start;
                padding: 0 20px;
                width: 100%;
                height: 60px;
            }

            // 移动端下拉菜单样式
            ul {}

            &.active {
                background-color: rgba(0, 0, 0, 0.1);
            }

            &:hover {
                background-color: rgba(0, 0, 0, 0.05);
            }
        }
    }
}
</style>